package com.gcloud.googledrive;

import java.io.IOException;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.List;

import com.example.gcode.Archivos;
import com.example.gcode.GCloudSingleton;
import com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential;
import com.google.api.services.drive.Drive;
import com.google.api.services.drive.Drive.Files;
import com.google.api.services.drive.model.File;
import com.google.api.services.drive.model.FileList;
import com.utils.GCloudUtil;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.sax.StartElementListener;
import android.util.Log;
import android.widget.Toast;

public class ListGoogleFile extends AsyncTask<Void, Long, Boolean> {

	private Drive service;
	private Context context;
	private final ProgressDialog dialog;
	GCloudSingleton gCloudSingleton;	
	GoogleAccountCredential credential;
	
	public ListGoogleFile(Drive service,
			Context context) {
		this.service = service;
		this.context = context;		
		this.credential = credential;
		dialog = new ProgressDialog(context);
		
		// if (context != null) {
		// dialog.setMax(100);
		// dialog.setMessage("Getting info from: " + service.getServicePath());
		// dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
		// dialog.setProgress(0);
		// dialog.show();
		// }
	}

	protected Boolean doInBackground(Void... params) {
		List<File> result = new ArrayList<File>();
		Files.List request = null;
		try {
			request = service.files().list();
			Log.i("retrieveAllFiles()", "getting files");
		} catch (IOException e1) {
			Log.i("retrieveAllFiles()", "something goes really wrong");
		}
		Log.i("retrieveAllFiles()", "to execute get Files");
		if (request == null) {
			Log.i("retrieveAllFiles() inside run",
					"to execute get Files inside run");
			return false;
		}
		Log.i("retrieveAllFiles()", "request well formed ");
		do {
			try {
				Log.i("ListGoogleFile", "Getting files");
				FileList files = request.execute();
				result.addAll(files.getItems());
				request.setPageToken(files.getNextPageToken());
				Log.i("ListGoogleFile", "Getting files at the end");
			} catch (SocketException s) {
				Log.i("ListGoogleFile", " SocketException: " + s);
				return false;
			} catch (IOException e) {
				System.out.println("An error occurred: " + e);
				request.setPageToken(null);
				return false;
			}
		} while (request.getPageToken() != null
				&& request.getPageToken().length() > 0);
		// TODO call singleton here

		if (result != null) {
			gCloudSingleton = GCloudSingleton.getInstance();
			gCloudSingleton.setFilesFromAllService(GCloudUtil
					.googleFilesToGCloudFiles(result));
		}
		// String datasFromFilesD = "";
		for (File file : result) {
			// datasFromFilesD += file.getTitle() + "  ";
			Log.i("RetrieveAllFiles", "File title: " + file.getTitle());
		}
		// showToast(datasFromFilesD);
		return true;
	}

	protected void onPostExecute(Boolean result) {
		dialog.dismiss();
		if (result) {
			Log.i("ListGoogleFile", "getting list success");
			showToast("Getting list success");
		} else {
			
			
			
//			Intent in = new Intent(null, Archivos.class);
//			in.putExtra("credential", true);
//			Activity ac = (Activity) this.context;
//			ac.startActivity(in);
			Log.i("ListGoogleFile", "getting list unsuccess");
			showToast("Getting List unsuccess");			
		}
	}

	
	
	private void showToast(String message) {
		if (context != null) {
			Toast toast = Toast.makeText(context, message, Toast.LENGTH_LONG);
			toast.show();
		}
	}

}
